# _*_coding:utf-8_*_
"""
该文件用于对mongo 中的数据查询，解析，并增加，更新 对应的解析后的字段


"""


import asyncio
import os
import sys
import time
import json
import traceback

project_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
print(project_path)
sys.path.append(project_path)

# 打印所有python解释器可以搜索到的所有路径
print(sys.path)


# from Freepatentsonline.databases.mongo_handler import Mongo_handler
# from Freepatentsonline.extract.extract_async import Extract
# from Freepatentsonline.settings import (CONFIG_DIR, CONFIG_FILENAME,
#                                         DB_CONFIG_FILENAME)
# from Freepatentsonline.utils.path_file import get_config




# # 数据库配置文件
# DB_CONFIG = get_config(CONFIG_DIR, DB_CONFIG_FILENAME)



# # 获取到 mongo 存储数据库配置信息
# MONGO_CONFIG = DB_CONFIG["MONGO"]
from Freepatentsonline.project_utils.extract_update_all import Extract_update

UPDATA_FILEDS = [
        "extract_status",
        "description",
        "parent_case_data"
                    
                 ]

class Extract_update_filed(Extract_update):
    def __init__(self) -> None:

        super().__init__()

        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.run())



    async def run(self):
        """"""
        collection_names = self.mongo.db.list_collection_names()
        
        total_count = 0
        # 存储更新异常的数据
        error_li = []

        for collection_name in collection_names:
            # collection_name = "75"
            # collection_name = _collection["name"]
            print(f"当前collection :{collection_name}")
            collection = self.mongo.db[collection_name]
            # 查询数据
            while 1:
                data_li = collection.find({"$or":[
                                                    {"extract_status": {"$exists": False}},
                                                    {"extract_status":1},
                                                    {"extract_status":2},
                                                    {"extract_status":3},
                                                    {"extract_status":4},
                                                  ]}, 
                                          {"task_id": 1, "source_text": 1, }
                                          ).limit(100)
                                    

                count = 0
                for data in data_li:
                    filter_data, extract_result = await self.handle_data(data)

                    extract_result_item = {}
                    # 读取需要修改的字段，从而构建新的更新数据包
                    for _filed in UPDATA_FILEDS:
                        if extract_result.get(_filed):
                            extract_result_item[_filed] = extract_result[_filed]
                    # 如果没有需要更新的字段 也需要更新解析 状态码
                    # if not extract_result_item:
                    #     print("无需更新，跳过...")
                    #     continue
                    update_data = {'$set':  extract_result_item}

                    # print(filter_data)
                    # print("-"*100)
                    # print(update_data)
                    try:
                        
                        collection.update_one(filter_data, update_data)
                        count += 1 
                        total_count +=1
                        print(f"更新完成：collection_name:{collection_name},_id:{filter_data['_id']} {total_count}条...")
                    except:
                        print(traceback.format_exc())
                        filter_data["_id"] = str(filter_data["_id"])
                        error_info = {
                            "filter_data":filter_data,
                            "update_data":update_data,
                            "traceback_info":traceback.format_exc()
                        }
                        print(error_info)
                        error_li.append(error_info)
  
                        
                        

                if count < 100:
                    break
                
            # break
        await self.log_error(error_li)


   
if __name__ == "__main__":
    extract_update = Extract_update_filed()
